package com.bestcem.xm.user.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.om.grpc.v1.services.CreateOperateLogRequest;
import com.bestcem.xm.om.grpc.v1.services.CreateOperateLogResponse;
import com.bestcem.xm.om.grpc.v1.services.OperateLogServiceGrpc;
import com.bestcem.xm.user.service.dto.user.UserOperateLogDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * 用户操作日志grpc service
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/11 13:53
 */
@Slf4j
@Service
public class UserOperateLogGrpcClientService {

    @GrpcClient(GrpcClientNames.DS_USER)
    private OperateLogServiceGrpc.OperateLogServiceBlockingStub stub;

    /**
     * 创建一条操作日志
     *
     * @param operateLogDTO
     * @return java.lang.Boolean
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/7/11 14:23
     */
    public Boolean createOperateLog(UserOperateLogDTO operateLogDTO) {
        // 设置请求参数
        CreateOperateLogRequest.Builder builder = CreateOperateLogRequest.newBuilder();
        builder.setUserId(operateLogDTO.getUserId())
                .setCategoryValue(operateLogDTO.getCategory())
                .setOperationValue(operateLogDTO.getOperation())
                .setOperateId(operateLogDTO.getOperateeId())
                .setTargetId(operateLogDTO.getTargetId())
                .setMsg(operateLogDTO.getMsg());

        // 请求
        CreateOperateLogResponse response = stub.createOperateLog(builder.build());

        // 返回值处理
        if (response != null && Code.OK_VALUE == response.getStatus().getCodeValue()) {
            if (log.isInfoEnabled()) {
                log.info("创建一条操作日志。response:{}", response);
            }
            return Boolean.TRUE;
        } else {
            log.error("创建一条操作日志失败, response:{}", response != null ? response.toString() : null);
        }
        return Boolean.FALSE;
    }

}
